interface Opt {
    yy: string,
    mm: string,
    dd: string,
    HH: string,
    MM: string,
    SS: string
  }
  
  export function formatDate(timestamp: string, format = 'yyyy-mm-dd') {
    let res = ""
    try {
      const date = new Date(timestamp)
      const opt: Opt = {
        yy: date.getFullYear().toString(),
        mm: (date.getMonth() + 1).toString(),
        dd: date.getDate().toString(),
        HH: date.getHours().toString(),
        MM: date.getMinutes().toString(),
        SS: date.getSeconds().toString()
      }
      // y+ 表示匹配1个或者多个y
      const regKeys: string[] = ['y+', 'm+', 'd+', 'H+', 'M+', 'S+']
      for (let i = 0; i < regKeys.length; i++) {
        const regKey = regKeys[i]
        const reg = new RegExp(regKey)
        let ret = reg.exec(format)
        if (ret) {
          switch (regKey) {
            case 'y+':
              format = format.replace(reg, ret.length !== 1 ? opt.yy : opt.yy.padStart(4, '0'))
              break
            case 'm+':
              format = format.replace(reg, ret.length !== 1 ? opt.mm : opt.mm.padStart(2, '0'))
              break
            case 'd+':
              format = format.replace(reg, ret.length !== 1 ? opt.dd : opt.dd.padStart(2, '0'))
              break
            case 'H+':
              format = format.replace(reg, ret.length !== 1 ? opt.HH : opt.HH.padStart(2, '0'))
              break
            case 'M+':
              format = format.replace(reg, ret.length !== 1 ? opt.MM : opt.MM.padStart(2, '0'))
              break
            case 'S+':
              format = format.replace(reg, ret.length !== 1 ? opt.SS : opt.SS.padStart(2, '0'))
              break
            default:
              break
          }
        }
  
      }
      res = format
    } catch (err) {
      console.log(err)
    }
    return res
  }

  // 校验字符长度
  export function validateChineseLength(str: string, max: number = 40) {
    // 使用正则表达式匹配汉字，一个汉字通常占两个字节，这里计算汉字数量
    let chineseCount = str.match(/[\u4e00-\u9fff]/g)?.length || 0;
    let englishCount = str.length - chineseCount;
    return chineseCount * 2 + englishCount <= max;
}